﻿<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8" />
    <title>Ping: src/main/java/com/stateforge/statemachine/examples/ping/EchoServerHandler.java Source File</title>
    <link href="/Content/Help.css" rel="stylesheet" type="text/css" />
    <link href="/Content/css/tabs.css" rel="stylesheet" type="text/css" />
    <link href="/Content/css/doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
    <div id="header">
        <img id="logo" alt="StateForge" src="/Content/Images/StateForgeLogo.png" />
        <div id="title">
            <h1>
                Examples using state machines generated by StateBuilderJava
            </h1>
            <h3>
                Ping: src/main/java/com/stateforge/statemachine/examples/ping/EchoServerHandler.java Source File
            </h3>
        </div>
        <div id="menucontainer">
            <ul id="menu">
                <li><a href="/">Home</a></li>
                <li><a href="/Home/Download">Downloads</a></li>
                <li><a href="/Help/state-machine-tools.aspx">Help</a></li>
                <li><a href="/Home/Faq">F.A.Q</a></li>
                <li><a href="/Home/About">Contact us</a></li>
            </ul>
        </div>
    </div>
<!-- Generated by Doxygen 1.7.4 -->
  <div id="navrow1" class="tabs">
    <ul class="tablist">
      <li><a href="index.html"><span>Main&#160;Page</span></a></li>
      <li><a href="namespaces.html"><span>Packages</span></a></li>
      <li><a href="annotated.html"><span>Classes</span></a></li>
      <li class="current"><a href="files.html"><span>Files</span></a></li>
    </ul>
  </div>
  <div id="navrow2" class="tabs2">
    <ul class="tablist">
      <li><a href="files.html"><span>File&#160;List</span></a></li>
    </ul>
  </div>
<div class="header">
  <div class="headertitle">
<div class="title">src/main/java/com/stateforge/statemachine/examples/ping/EchoServerHandler.java</div>  </div>
</div>
<div class="contents">
<a href="_echo_server_handler_8java.html">Go to the documentation of this file.</a><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 <span class="comment">/*</span>
<a name="l00002"></a>00002 <span class="comment"> * Copyright 2009 Red Hat, Inc.</span>
<a name="l00003"></a>00003 <span class="comment"> *</span>
<a name="l00004"></a>00004 <span class="comment"> * Red Hat licenses this file to you under the Apache License, version 2.0</span>
<a name="l00005"></a>00005 <span class="comment"> * (the &quot;License&quot;); you may not use this file except in compliance with the</span>
<a name="l00006"></a>00006 <span class="comment"> * License.  You may obtain a copy of the License at:</span>
<a name="l00007"></a>00007 <span class="comment"> *</span>
<a name="l00008"></a>00008 <span class="comment"> *    http://www.apache.org/licenses/LICENSE-2.0</span>
<a name="l00009"></a>00009 <span class="comment"> *</span>
<a name="l00010"></a>00010 <span class="comment"> * Unless required by applicable law or agreed to in writing, software</span>
<a name="l00011"></a>00011 <span class="comment"> * distributed under the License is distributed on an &quot;AS IS&quot; BASIS, WITHOUT</span>
<a name="l00012"></a>00012 <span class="comment"> * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the</span>
<a name="l00013"></a>00013 <span class="comment"> * License for the specific language governing permissions and limitations</span>
<a name="l00014"></a>00014 <span class="comment"> * under the License.</span>
<a name="l00015"></a>00015 <span class="comment"> */</span>
<a name="l00016"></a>00016 <span class="keyword">package </span>com.stateforge.statemachine.examples.ping;
<a name="l00017"></a>00017 
<a name="l00018"></a>00018 <span class="keyword">import</span> java.util.concurrent.atomic.AtomicLong;
<a name="l00019"></a>00019 <span class="keyword">import</span> java.util.logging.Level;
<a name="l00020"></a>00020 <span class="keyword">import</span> java.util.logging.Logger;
<a name="l00021"></a>00021 
<a name="l00022"></a>00022 <span class="keyword">import</span> org.jboss.netty.buffer.ChannelBuffer;
<a name="l00023"></a>00023 <span class="keyword">import</span> org.jboss.netty.channel.ChannelHandlerContext;
<a name="l00024"></a>00024 <span class="keyword">import</span> org.jboss.netty.channel.ExceptionEvent;
<a name="l00025"></a>00025 <span class="keyword">import</span> org.jboss.netty.channel.MessageEvent;
<a name="l00026"></a>00026 <span class="keyword">import</span> org.jboss.netty.channel.SimpleChannelUpstreamHandler;
<a name="l00027"></a>00027 
<a name="l00036"></a><a class="code" href="classcom_1_1stateforge_1_1statemachine_1_1examples_1_1ping_1_1_echo_server_handler.html">00036</a> <span class="keyword">public</span> <span class="keyword">class </span><a class="code" href="classcom_1_1stateforge_1_1statemachine_1_1examples_1_1ping_1_1_echo_server_handler.html">EchoServerHandler</a> <span class="keyword">extends</span> SimpleChannelUpstreamHandler {
<a name="l00037"></a>00037 
<a name="l00038"></a><a class="code" href="classcom_1_1stateforge_1_1statemachine_1_1examples_1_1ping_1_1_echo_server_handler.html#a539f8b5bb01d1f03d0b4e99c80ea110e">00038</a>     <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> Logger <a class="code" href="classcom_1_1stateforge_1_1statemachine_1_1examples_1_1ping_1_1_echo_server_handler.html#a539f8b5bb01d1f03d0b4e99c80ea110e">logger</a> = Logger.getLogger(
<a name="l00039"></a>00039             <a class="code" href="classcom_1_1stateforge_1_1statemachine_1_1examples_1_1ping_1_1_echo_server_handler.html">EchoServerHandler</a>.class.getName());
<a name="l00040"></a>00040 
<a name="l00041"></a><a class="code" href="classcom_1_1stateforge_1_1statemachine_1_1examples_1_1ping_1_1_echo_server_handler.html#a30e3482790e9db81e1fde2dcd4c24c1b">00041</a>     <span class="keyword">private</span> <span class="keyword">final</span> AtomicLong <a class="code" href="classcom_1_1stateforge_1_1statemachine_1_1examples_1_1ping_1_1_echo_server_handler.html#a30e3482790e9db81e1fde2dcd4c24c1b">transferredBytes</a> = <span class="keyword">new</span> AtomicLong();
<a name="l00042"></a>00042 
<a name="l00043"></a><a class="code" href="classcom_1_1stateforge_1_1statemachine_1_1examples_1_1ping_1_1_echo_server_handler.html#a8e3cf4e4cc603aa1305b455d465cae79">00043</a>     <span class="keyword">public</span> <span class="keywordtype">long</span> <a class="code" href="classcom_1_1stateforge_1_1statemachine_1_1examples_1_1ping_1_1_echo_server_handler.html#a8e3cf4e4cc603aa1305b455d465cae79">getTransferredBytes</a>() {
<a name="l00044"></a>00044         <span class="keywordflow">return</span> <a class="code" href="classcom_1_1stateforge_1_1statemachine_1_1examples_1_1ping_1_1_echo_server_handler.html#a30e3482790e9db81e1fde2dcd4c24c1b">transferredBytes</a>.get();
<a name="l00045"></a>00045     }
<a name="l00046"></a>00046 
<a name="l00047"></a>00047     @Override
<a name="l00048"></a><a class="code" href="classcom_1_1stateforge_1_1statemachine_1_1examples_1_1ping_1_1_echo_server_handler.html#a276bef264e7930a53711fe2c6a8d1cec">00048</a>     <span class="keyword">public</span> <span class="keywordtype">void</span> <a class="code" href="classcom_1_1stateforge_1_1statemachine_1_1examples_1_1ping_1_1_echo_server_handler.html#a276bef264e7930a53711fe2c6a8d1cec">messageReceived</a>(
<a name="l00049"></a>00049             ChannelHandlerContext ctx, MessageEvent e) {
<a name="l00050"></a>00050         <span class="comment">// Send back the received message to the remote peer.</span>
<a name="l00051"></a>00051         <a class="code" href="classcom_1_1stateforge_1_1statemachine_1_1examples_1_1ping_1_1_echo_server_handler.html#a30e3482790e9db81e1fde2dcd4c24c1b">transferredBytes</a>.addAndGet(((ChannelBuffer) e.getMessage()).readableBytes());
<a name="l00052"></a>00052         e.getChannel().write(e.getMessage());
<a name="l00053"></a>00053     }
<a name="l00054"></a>00054 
<a name="l00055"></a>00055     @Override
<a name="l00056"></a><a class="code" href="classcom_1_1stateforge_1_1statemachine_1_1examples_1_1ping_1_1_echo_server_handler.html#a2ca319c0635c124b66415106a16fdad5">00056</a>     <span class="keyword">public</span> <span class="keywordtype">void</span> <a class="code" href="classcom_1_1stateforge_1_1statemachine_1_1examples_1_1ping_1_1_echo_server_handler.html#a2ca319c0635c124b66415106a16fdad5">exceptionCaught</a>(
<a name="l00057"></a>00057             ChannelHandlerContext ctx, ExceptionEvent e) {
<a name="l00058"></a>00058         <span class="comment">// Close the connection when an exception is raised.</span>
<a name="l00059"></a>00059         <a class="code" href="classcom_1_1stateforge_1_1statemachine_1_1examples_1_1ping_1_1_echo_server_handler.html#a539f8b5bb01d1f03d0b4e99c80ea110e">logger</a>.log(
<a name="l00060"></a>00060                 Level.WARNING,
<a name="l00061"></a>00061                 <span class="stringliteral">&quot;Unexpected exception from downstream.&quot;</span>,
<a name="l00062"></a>00062                 e.getCause());
<a name="l00063"></a>00063         e.getChannel().close();
<a name="l00064"></a>00064     }
<a name="l00065"></a>00065 }
</pre></div></div>
</div>
﻿<div id="footer">
    <hr />
    <p>
        Copyright &copy; 2011 StateForge.</p>
    <hr />
</div>
</body> </html> 
